
import { request } from "../util/request.js"
export default {
    async get({ page, limit }) {
        return await request({
            type: "GET",
            url: "/api/students",
            data: { page, limit }
        });
    },
    async update(student) {
        return await request({
            type: "PUT",
            url: `/api/students/${student._id}`,
            data: student
        });
    },
    setInfo(name, data) {
        return sessionStorage[name] = JSON.stringify(data);
    },
    getInfo(name) {
        return JSON.parse(sessionStorage[name]);
    },
    async getClasses() {
        return await request({
            type: "GET",
            url: "/api/classes",
        })
    },
    async addstu(student) {
        return await request({
            type: "POST",
            url: "/api/students",
            data: student
        });
    },
    async getOne(_id) {
        return await request({
            type: "get",
            url: `/api/students/${_id}`,
            data: _id
        });
    },
    async deletestu(_id) {
        await request({
            type: "DELETE",
            url: `/api/students/${_id}`,
        });
    },
    async deleteImg({ _id, url }) {
        await request({
            type: "PATCH",
            url: `/resource/students/${_id}`,
            data: { url }
        });
    },





    //layui
    async layuilist({ toolbar, toolbarhead }) {
        return await layui.table.render({
            elem: '#stutable'
            , height: 526
            , method: "get"
            , url: '/api/layui'
            , page: true
            , limits: [5, 10, 15, 20]
            , cols: [[
                { type: 'numbers', title: '全选', fixed: 'left' }
                , { type: 'checkbox', fixed: 'left' }
                , { field: '_id', title: 'ID', width: 220 }
                , { field: 'name', title: '姓名', width: 80 }
                , { field: 'age', title: '年龄', width: 80, sort: true }
                , { field: 'sex', title: '性别', width: 80, sort: true }
                , { field: 'classId', title: '班级', width: 80, sort: true }
                , { field: 'tel', title: '电话', width: 120 }
                , { field: 'email', title: '邮箱' }
                , {
                    fixed: 'right', field: 'tool', title: '操作', width: 120, toolbar: toolbar
                }
            ]]
            , toolbar: toolbarhead
        });
    },
    async updateStu(data) {
        return await request({
            type: "PUT",
            url: `/api/layui/${data._id}`,
            data: data
        });
    },
    async select(data) {
        await layui.table.reload("stutable", {
            url: "/api/layui/select",
            where: data,
            page: { curr: 1 }
        });
    },
    async add(data) {
        await request({
            type: "POST",
            url: "/api/layui",
            data: data
        });
    },
    async reload(num = 0) {
        if (num) {
            await layui.table.reload("stutable", {
                type: "GET",
                url: "/api/layui",
                page: { curr: num }
            });
        } else {
            await layui.table.reload("stutable");
        }
    },
    async delete(_id) {
        await request({
            type: "DELETE",
            url: `/api/layui/${_id}`,
        });
    }
}